package com.medicine.hbp.adapter.self;

import java.util.List;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.http.HttpParams;

import com.medicine.hbp.base.MyBaseAdapter;
import com.medicine.hbp.emotion.StringUtils;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.ArtInfoVo;
import com.medicine.hbp.ui.WebActivity;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class SelfUserTgAdapter extends MyBaseAdapter {
	private LayoutInflater inflater;
	private KJBitmap bitmap = new KJBitmap();
	private int type;//1为显示删除按钮   0为不显示删除按钮
	public SelfUserTgAdapter(Activity activity,int type) {
		this.type=type;
		this.activity = activity;
		inflater = LayoutInflater.from(activity);
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.item_tg, null);
			holder.text_user_tg_nr = (TextView) view
					.findViewById(R.id.text_user_tg_nr);
			holder.img_user_tg_one = (ImageView) view
					.findViewById(R.id.img_user_tg_one);
			holder.img_user_tg_two = (ImageView) view
					.findViewById(R.id.img_user_tg_two);
			holder.img_delete_tg = (ImageView) view
					.findViewById(R.id.img_delete_tg);
			holder.txt_tg_state= (TextView) view
					.findViewById(R.id.txt_tg_state);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		final ArtInfoVo artInfoVo = (ArtInfoVo) getItem(position);
		if (artInfoVo != null) {
			holder.text_user_tg_nr.setText(StringUtils.getEmotionContent(
					activity, holder.text_user_tg_nr, artInfoVo.artMsg));
			if (artInfoVo.artImg.size() > 0) {
				if (!TextUtils.isEmpty(artInfoVo.artImg.get(0).artImgUrl)) {
					bitmap.display(holder.img_user_tg_one, artInfoVo.artImg.get(0).artImgUrl);
				}
			}
			if (artInfoVo.artImg.size() > 1) {
				if (!TextUtils.isEmpty(artInfoVo.artImg.get(1).artImgUrl)) {
					bitmap.display(holder.img_user_tg_two, artInfoVo.artImg.get(1).artImgUrl);
				}
			}
			if (artInfoVo.artImg.size() == 0) {
				holder.img_user_tg_one.setVisibility(View.GONE);
				holder.img_user_tg_two.setVisibility(View.GONE);
			} else if (artInfoVo.artImg.size() == 1) {
				holder.img_user_tg_one.setLayoutParams(new LayoutParams(view.getWidth()-20, view.getWidth()/5*3));
				holder.img_user_tg_one.setVisibility(View.VISIBLE);
				holder.img_user_tg_two.setVisibility(View.INVISIBLE);
			} else if (artInfoVo.artImg.size() > 1) {
				holder.img_user_tg_one.setVisibility(View.VISIBLE);
				holder.img_user_tg_two.setVisibility(View.VISIBLE);
			}
			if(artInfoVo.artStatus.equals("0")){//已审核
				holder.txt_tg_state.setText("审核已通过");
				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Intent intent = new Intent(activity, WebActivity.class);
						intent.putExtra("isCollect", artInfoVo.isCollect);
						intent.putExtra("ID", artInfoVo.artId);
						intent.putExtra("NAME", "文章内容详情");
						activity.startActivity(intent);

					}
				});
			}else if(artInfoVo.artStatus.equals("-1")){//未审核
				view.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						ToastUtil.showShort(activity, "文章正在审核中");
					}
				});
				holder.txt_tg_state.setText("审核未通过");
			}
			if(type==1){
				holder.img_delete_tg.setVisibility(View.VISIBLE);
			}else if(type==0){
				holder.img_delete_tg.setVisibility(View.GONE);
			}
			holder.img_delete_tg.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					Dialog(artInfoVo.artId);
				}
			});
		}

		return view;
	}

	class ViewHolder {
		/** 发布的内容 */
		TextView text_user_tg_nr;
		/** 审核状态显示 */
		TextView txt_tg_state;
		/** 图片1 */
		ImageView img_user_tg_one;
		/** 图片2 */
		ImageView img_user_tg_two;
		/** 删除按钮 */
		ImageView img_delete_tg;
	}

	private void Dialog(final int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle("提示");
		builder.setMessage("删除投稿");
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				httpDelArt(id);
			}
		});
		builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface arg0, int arg1) {

			}
		});
		builder.show();
	}

	/**
	 * 删除用户未审核通过的文章
	 * @param id
	 */
	private void httpDelArt(final int id) {
		HttpParams params = new HttpParams();
		params.put("artId", id);// 需要删除的文章ID
		AppHttps.HttpPost(activity,HttpInterface.DEL_ART, params,
				new AppHttpCallBack<Object>(Object.class) {

					@Override
					public void onSuccess(Object result) {
						List<ArtInfoVo> infoVos=getItems();
						for (int i = 0; i < infoVos.size(); i++) {
							if(infoVos.get(i).artId==id){
								infoVos.remove(i);
								break;
							}
						}
						setItems(infoVos);
						notifyDataSetChanged();
						ToastUtil.showShort(activity, "文章删除成功");
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(activity, errorMsg);
					}
				});

	}

}
